package com.forum.common.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RabbitMQ通用配置类
 * 确保所有服务使用相同的消息转换器
 */
@Configuration
public class RabbitMQConfig {
    
    /**
     * 配置消息转换器
     * 使用JSON格式序列化和反序列化消息，支持Java 8日期时间类型
     */
    @Bean
    public MessageConverter jsonMessageConverter() {
        // 创建支持Java 8时间类型的ObjectMapper
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        
        // 创建Jackson2JsonMessageConverter并设置ObjectMapper
        Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter(objectMapper);
        return converter;
    }
} 